package util;

public class LangUtils {
	/**
	 * The as operator is used to perform conversions between compatible types. The as operator is like a cast except
	 * that it yields null on conversion failure instead of raising an exception. Is equivalent to:
	 * 
	 * <pre>
     * expression instanceof type
     *         ? (type) expression
     *         : (type) null
     * </pre>
	 * 
	 * PS. There are few good things in C#
	 * 
	 * @param object
	 * @return casted object or null
	 * @author Mykhaylo Adamovych
	 */
	@SuppressWarnings("unchecked")
	public static <T> T as(Class<T> clazz, Object object) {
		if (object == null)
			return null;
		if (clazz.isAssignableFrom(object.getClass()))
			return (T) object;
		return null;
	}
}
